﻿using System;
using System.Collections.Generic;
 
using System.Web;
using CityGatway;
using System.ServiceModel.Activation;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using CityGateway.Objects;
using System.Security.AccessControl;

/// <summary>
/// Summary description for CityService
/// </summary>
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class CityService :ICityService
{
    public CityService()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    #region ICityService Members
    public FacebookSession GetSession()
    {
        return new FacebookSession();

    }


    public CityStatus GetCityStatus(long userid)
    {

        CityStatus cityStatus = null;
        if (userid != 0)
        {
            string filepath = HttpContext.Current.Server.MapPath("~/Cities/cityStatus_" + userid.ToString());
            if (File.Exists(filepath))
            {
                FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read);

                BinaryFormatter bformatter = new BinaryFormatter();


                cityStatus = (CityStatus)bformatter.Deserialize(fs);
                fs.Close();
                fs.Dispose();

                return cityStatus;
            }
        }


        cityStatus = new CityStatus();

  
 
        return cityStatus;
        

    }

    public City GetPlayerCity(long userid)
    {
        City city = null;
        if (userid != 0)
        {
            string filepath = HttpContext.Current.Server.MapPath("~/Cities/city_" + userid.ToString());
            if (File.Exists(filepath))
            {
                FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read);
       
                BinaryFormatter bformatter = new BinaryFormatter();
                

                city = (City)bformatter.Deserialize(fs);
                fs.Close();
                fs.Dispose();


                  filepath = HttpContext.Current.Server.MapPath("~/Cities/cityStatus_" + userid.ToString());
                  if (File.Exists(filepath))
                  {
                      fs = new FileStream(filepath, FileMode.Open, FileAccess.Read);

                      bformatter = new BinaryFormatter();


                      city.CityStatus = (CityStatus)bformatter.Deserialize(fs);
                      fs.Close();
                      fs.Dispose();
                  }


                  foreach (long l in city.Neighboures.Keys)
                  {
                      city.Neighboures[l].CityScore =Convert.ToInt32( GetCityStatus(l).CityScore.ToString());                    

                  }


                return city;
            }
       

            city = new City();
            city.UserID = userid;

            city.Budget = new Budget();
            //income
            city.Budget.Commercial = 9;
            city.Budget.Industrial = 9;
            city.Budget.Residential = 9;

            //expenses
            city.Budget.Police = 100;
            city.Budget.Education = 100;
            city.Budget.Energy = 100;
            city.Budget.FireDepartment = 100;
            city.Budget.HealthCare = 100;
            city.Budget.Transportation = 100;
            city.Budget.Waste = 100;
            city.Budget.Water = 100;



            return city;
        }
        return null;
     

    }

    public bool SaveCity(City city)
    {

        if (city.UserID > 0)
        {
            string filepath = HttpContext.Current.Server.MapPath("~/Cities/city_" + city.UserID.ToString());
            FileStream fs = new FileStream(filepath, FileMode.OpenOrCreate);
            BinaryFormatter bformatter = new BinaryFormatter();
            
            bformatter.Serialize(fs, city);
            fs.Close();
            fs.Dispose();
            
           // System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(city.GetType());
           // x.Serialize(fs, city);

            return true;
        }

        return false;
    }



    public Dictionary<long,CityScore> GetNeighbours(List<long> neighbours)
    {

        Dictionary<long, CityScore> res = new Dictionary<long, CityScore>();
        foreach (long l in neighbours)
        {
            res.Add(l, GetCityStatus(l).CityScore);

        }

        return res;


    }

    #endregion
}
